!! 纯数组快于数组元素访问，矢量化快于 do 循环

!> author: 左志华
!> date: 2022-10-05
program main

    use sph_kinds, only: rk
    use seakeeping_time, only: tic, toc
    implicit none

    type loc_t
        real(rk) :: x, y
    end type loc_t
    integer, parameter :: N = 50000000
    type(loc_t) :: particle(N)
    real(rk) :: particle_x(N), particle_y(N)
    integer :: i, seed

    call tic(seed)
    do i = 1, N
        particle(i)%x = 1.0_rk
        particle(i)%y = 2.0_rk
    end do
    call toc(seed, "type with loop")

    call tic(seed)
    particle(:)%x = 1.0_rk
    particle(:)%y = 2.0_rk
    call toc(seed, "type with vectorization")

    call tic(seed)
    do i = 1, N
        particle_x(i) = 1.0_rk
        particle_y(i) = 2.0_rk
    end do
    call toc(seed, "array with loop")

    call tic(seed)
    particle_x(:) = 1.0_rk
    particle_y(:) = 2.0_rk
    call toc(seed, "array with vectorization")

end program main

!> Result:
! type with loop, time elapsed: 0.407 s
! type with vectorization, time elapsed: 0.172 s
! array with loop, time elapsed: 1.53 s
! array with vectorization, time elapsed: 0.110 s